package com.base.commons.log.config;

import com.base.commons.log.CommonConstant;
import com.base.commons.tools.utils.StringUtils;
import feign.RequestInterceptor;
import org.slf4j.MDC;
import org.springframework.context.annotation.Bean;

/**
 * @author daiguoping
 * @version 1.00
 * @time 2019/12/27 0027
 * @description
 */
//@Configuration
public class TraceWebMvcConfig {

    /**
     * 针对的普通服务
     * @return
     */


    /**
     * 针对的是网关服务
     * @return
     */
//    @Bean
//    public FilterRegistrationBean traceFilter() {
//        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
//        TraceFilter traceFilter = new TraceFilter();
//        filterRegistrationBean.setFilter(traceFilter);
//        List<String> urls = new ArrayList<>();
//        urls.add("/*");
//        //配置过滤规则
//        filterRegistrationBean.setUrlPatterns(urls);
//        filterRegistrationBean.setOrder(-100000);
//        return filterRegistrationBean;
//    }

    @Bean
    public RequestInterceptor requestInterceptor() {
        RequestInterceptor requestInterceptor = template -> {
            //传递日志traceId
            String traceId = MDC.get(CommonConstant.LOG_TRACE_ID);
            if (StringUtils.isNotEmpty(traceId)) {
                template.header(CommonConstant.TRACE_ID_HEADER, traceId);
            }
        };
        return requestInterceptor;
    }

}
